package com.tengju.data.domain.model.report;


import com.tengju.data.domain.model.template.SubsidyTemplate;
import com.tengju.data.domain.model.user.UserLevelInfo;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.model.user.UserStarFeature;
import com.tengju.data.domain.shared.IdObject;
import com.tengju.data.domain.shared.tree.TreeData;
import lombok.Getter;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Getter
public class MarketReport implements TreeData {

    private UserIdCode leaderIdCode;

    private UserLevelInfo leaderUserInfo;

    private MarketAchievement marketAchievement;

    private SmallTeamAchievement smallTeamAchievement;

    private Map<Integer, SubsidyTemplate> subsidyTemplateMap;

    private LocalDateTime logTime;

    private Integer month;

    private boolean isCompleted = false;

    private UserStarFeature starFeature;

    public MarketReport(UserIdCode leaderIdCode,
                        UserLevelInfo leaderUserInfo,
                        MarketAchievement marketAchievement,
                        SmallTeamAchievement smallTeamAchievement,
                        Map<Integer, SubsidyTemplate> subsidyTemplateMap,
                        LocalDateTime logTime,
                        Integer month,
                        UserStarFeature starFeature) {
        this.leaderIdCode = leaderIdCode;
        this.leaderUserInfo = leaderUserInfo;
        this.marketAchievement = marketAchievement;
        this.smallTeamAchievement = smallTeamAchievement;
        this.subsidyTemplateMap = subsidyTemplateMap;
        this.logTime = logTime;
        this.month = month;
        this.starFeature = starFeature;
    }

    @Override
    public IdObject getId() {
        return leaderIdCode;
    }

    /**
     * 下级大团队的出货额合并自身小团队的出货额
     */
    private void calSelfAchievement() {
        List<SmallTeamAchievement.Achievement> smallTeamAchievementList = smallTeamAchievement.getAchievementList();
        if (CollectionUtils.isEmpty(smallTeamAchievementList)) {
            return;
        }
        for (SmallTeamAchievement.Achievement smallTeamAchievement : smallTeamAchievementList) {
            Integer teamRatioType = smallTeamAchievement.getTeamRatioType();
            marketAchievement.addSelfTeamAchievement(teamRatioType,
                    smallTeamAchievement.getSelfConsumeIndex(),
                    smallTeamAchievement.getConsumeIndex(),
                    smallTeamAchievement.getSmallTeamCancelMoneyOut(),
                    smallTeamAchievement.getSmallTeamCancelBeforeMoneyOut()
            );
        }
    }

    /**
     * 合并下级大团队的出货额
     * @param childrenReports
     */
    private void combineChildrenAchievement(@NotNull List<MarketReport> childrenReports) {
        for (MarketReport childrenReport : childrenReports) {
            MarketAchievement childBigTeamAchievement = childrenReport.getMarketAchievement();
            marketAchievement.combineChildAchievement(childBigTeamAchievement);
        }
    }

    public List<AchievementBonus> printMarketBonus(int version) {
        return marketAchievement.printBonus(leaderUserInfo,subsidyTemplateMap,starFeature,version);
    }

    @Override
    public String toString() {
        return "MarketReport{" +
                "leaderIdCode=" + leaderIdCode.toString() +
                '}';
    }

    /**
     * 准备市场补贴计算数据
     * 1. 大团队出货额
     * 2. 商品补贴系数
     * @param childrenReports
     */
    public void prepareData(List<MarketReport> childrenReports) {
        combineChildrenAchievement(childrenReports);
        calSelfAchievement();
        marketAchievement.calTypeDebate(subsidyTemplateMap,leaderUserInfo,starFeature);
    }

    /**
     * 计算大团队市场补贴
     * 1.
     * @return
     */
    public void calBonus() {
        marketAchievement.doCalBonus();
    }

    public Set<Integer> getTeamTypeSet() {
        return marketAchievement.getTypeAchievementMap().keySet();
    }


    public boolean isBelongTo(MarketReport other, Integer teamRatioType) {
        if (other == null || other.getMarketAchievement() == null) {
            return false;
        }
        double selfBonusDebate = this.getMarketAchievement().getBonusDebate(teamRatioType);
        double otherBonusDebate = other.getMarketAchievement().getBonusDebate(teamRatioType);
        return BigDecimal.valueOf(selfBonusDebate).compareTo(BigDecimal.valueOf(otherBonusDebate)) <= 0;
    }

    public void setSettleParent(Integer teamRatioType, UserLevelInfo settleParentLeaderUserInfo) {
        this.marketAchievement.setSettleParent(teamRatioType,settleParentLeaderUserInfo);
    }

    public void combineSettleAchievement(Integer teamRatioType, MarketReport marketReport) {
        this.marketAchievement.combineSettleAchievementAndBonus(teamRatioType,marketReport.getMarketAchievement());
    }

    public void completeCal() {
        this.isCompleted = true;
    }
}
